package com.clouddot.baipai.ui.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/17.
 */

public class ListDataSaveUtil {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public ListDataSaveUtil(Context mContext, String preferenceName) {
        preferences = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    /**
     * 保存List
     * @param tag
     * @param datalist
     */
    public <T> void setDataList(String tag, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0)
            return;

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        editor.clear();
        editor.putString(tag, strJson);
        editor.commit();
    }
    /**
     * 获取List
     * @param tag
     * @return
     */
    public <T> List<T> getDataListFromBase(String tag,Class<T> cls) {
        List<T> datalist=new ArrayList<T>();
        try {
            Gson gson = new Gson();
            String strJson = preferences.getString(tag, null);
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                datalist.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datalist;

    }


    /**
     * 获取List
     * @param strJson
     * @return
     */
    public static  <T> List<T> getDataListFromNet(String strJson,Class<T> cls) {
        List<T> datalist = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                datalist.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datalist;

    }


    /**
     * 获取map
     * @param strJson
     * @return
     */
    public static <T> Map<String,Object> getDataDifListFromNet(String strJson, List<Type> clss, String[] elem) {
        Map<String,Object> map = new HashMap<String ,Object>();
        List<Object> list = new ArrayList<Object>();
        try {
            Gson gson = new Gson();
            JsonObject jsonObject = new JsonParser().parse(strJson).getAsJsonObject();
            for(int i = 0 ;i < elem.length; i++){
                JsonElement element = jsonObject.get(elem[i]);
                if(element.isJsonArray()){
                    JsonArray array = element.getAsJsonArray();
                    if(array.size() > 0){
                        for(JsonElement element1 : array){
                            list.add(gson.fromJson(element1, clss.get(i)));
                        }
                        map.put(elem[i],list);
                    }
                }else{
                   map.put(elem[i], gson.fromJson(element, clss.get(i)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }


    public void clear(String tag){
        editor.remove(tag);
        editor.commit();
    }
}